/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.wholeconsulting.pacc.gui.fluidos;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.table.DefaultTableModel;

import ar.com.wholeconsulting.pacc.model.Fluido;

/**
 *
 * @author Duco Pablo
 */
public class FluidoModel extends DefaultTableModel {
    /**
	 * 
	 */
	private static final long serialVersionUID = -4048369916473167015L;

	public FluidoModel(Object[][] data, Object[] columnNames) {
        super(data, columnNames);
    }

    public void addRow(Fluido fluido) {
        Object[] vector = new Object[10];
        try {
            for (FluidoColum fluid : FluidoColum.values()) {
                Method method = Fluido.class.getMethod(fluid.getterMethodName());
                vector[fluid.getIndex()] = method.invoke(fluido);
            }
            vector[0]=fluido;
            super.addRow(vector);
        } catch (Exception ex) {
            Logger.getLogger(FluidoModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void setValueAt(Object aValue, int row, int column) {
        try {
            FluidoColum fluid = FluidoColum.fromIndex(column);
            for (Method method : Fluido.class.getMethods()) {
                if (method.getName().equals(fluid.setterMethodName())) {
                    Object obj = super.getValueAt(row, 0);
                    method.invoke(obj, aValue);
                }
            }
            //super.setValueAt(aValue, row, column);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(FluidoModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(FluidoModel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Override
    public Object getValueAt(int row, int column) {
        try {
            FluidoColum fluid = FluidoColum.fromIndex(column);
            //fluid.getterMethodName()
            Method method = Fluido.class.getMethod(fluid.getterMethodName());
            return method.invoke(super.getValueAt(row, 0));
        } catch (IllegalAccessException ex) {
            Logger.getLogger(FluidoModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(FluidoModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(FluidoModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(FluidoModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(FluidoModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        throw new IllegalArgumentException();
    }
}
